rm(list = ls())
dev.off()
### Read-in #####
require(reshape2)
library(tidyverse)
library(grid)
library(gridExtra)
library(png)
require(cowplot)
library(scales)

df <-readRDS("pooled_long.RDS")

df.party <- df[(df$republican==1|df$democrat==1),]

df.party$type <- factor(ifelse(df.party$elite == 1,"Public","Government officials"),
                        levels = c("Public","Government officials"))

df.party$party_bin = factor(ifelse(df.party$republican == 1,"Republican","Democrat"),
                            levels = c("Republican","Democrat"))
###  Figure 2 prep ####
d = "Voter\nfraud"
a = "Health care\nspending"
b = "Tax share\nfrom 1%"
e =  "Climate\nchange"
f = "Needle\nexchanges"
c = "GMO\nsafety"
g= "Foreign-born\nnational rate"
h = "Unemployment\nnational rate"
issues = c("fraud","warming","militaryHC","taxes","NEP","GMO","unemp_US","foreign_US")
results = data.frame(matrix(,16,4))
colnames(results) = c("issue","type","mean","ci")
results[,"issue"] = rep(issues,2)
results[,"type"] = paste(c(rep("Government officials",8), rep("Public",8)))
for(i in 1:length(issues)){
reg = lm(dv ~ elite, #+ republican + democrat + nonwhite  + age_3044 +  age_4564 + age_65plus + college + female + nonwhite, 
         data=df[df$issue==issues[i],])
results[i,"mean"] = summary(reg)$coefficients["elite",1]
results[i,"ci"] = 1.96*summary(reg)$coefficients["elite",2]
}

results$issue[results$issue=="fraud"] = d
results$issue[results$issue=="taxes"] =  b
results$issue[results$issue=="militaryHC"] = a
results$issue[results$issue=="warming"] =e
results$issue[results$issue=="NEP"] = f
results$issue[results$issue=="GMO"] = c
results$issue[results$issue=="foreign_US"] = g
results$issue[results$issue=="unemp_US"] = h

issues = c(h,g,f,e,d,c,b,a)
results$issue = factor(results$issue,levels = issues)

results = results[1:8,]

#### Figure 2 #####
ggplot(data=results, aes(y=as.numeric(mean),x = issue,shape=type))+#,shape = type))+
  geom_point(stat='identity',size=2.5)+
   geom_errorbar(aes(ymin=mean - ci,ymax=mean + ci),
  position = position_dodge(width = 0, preserve = c("total")), width=0,alpha=0.8)+
  geom_hline(yintercept=0,lwd=0.2,lty=2)+
 # facet_wrap(~ issue,nrow = 8,switch = "y")+
   scale_shape_manual(name = "Respondent", values = c("Public"  = 18,"Government officials" = 15))+
   theme_minimal()+#theme(axis.text.y=element_blank(), axis.ticks.y=element_blank())+
   theme(legend.position="none",legend.box = "vertical",
         legend.justification =  0.5, legend.spacing.x = unit(0.1,"cm"),
         legend.spacing.y = unit(-0.5,"cm"))+xlab('')+ 
   ylab('\nAccuracy of government officials relative to public')+
  ylim(-0.15,0.3) +
   coord_flip()+
   theme(text = element_text(size=11))+#axis.text.y = element_blank(),plot.title = element_blank())+
ggsave("fig_2.pdf",width=7.5, height=5)


### Figure 1 Prep #####
issues = c("fraud","warming","militaryHC","taxes","NEP","GMO","unemp_US","foreign_US")
results = data.frame(matrix(,32,5))
colnames(results) = c("issue","party","type","mean","ci")
results$party = factor(rep(c("Democrat","Republican"),16),
                       levels = c("Republican","Democrat"))
results$type = factor(rep(c("Public","Public","Government officials","Government officials"),8),
                      levels = c("Public","Government officials"))

for (i in 1:8){
  k = 4*i - 3
  results[k:(k + 3),"issue"] = paste(rep(issues[i],4))
  reg = lm(dv ~ elite*republican, #+ independent+nonwhite  + age_3044 +  age_4564 + age_65plus + college + female + nonwhite, 
           data=df[df$issue==issues[i],])
  
  results[k,"mean"] =  -0.5*summary(reg)$coefficients["republican",1]
  results[k + 1,"mean"] = summary(reg)$coefficients["republican",1] - 0.5*summary(reg)$coefficients["republican",1]
  results[k + 2,"mean"] = summary(reg)$coefficients["elite",1]
  results[k + 3,"mean"] = summary(reg)$coefficients["elite",1] + summary(reg)$coefficients["republican",1]  +
    summary(reg)$coefficients["elite:republican",1]
  results[k,"ci"] = NA
  results[k + 1,"ci"] =  1.96*summary(reg)$coefficients["republican",2]
  results[k + 2,"ci"] = 1.96*summary(reg)$coefficients["elite",2]
  results[k + 3,"ci"] = 1.96*sqrt(vcov(reg)["elite","elite"]+vcov(reg)["republican","republican"]+vcov(reg)["elite:republican","elite:republican"] +2*vcov(reg)["elite","republican"]+2*vcov(reg)["elite:republican","elite"]+2*vcov(reg)["elite:republican","republican"])
}

results$issue[results$issue=="fraud"] = d
results$issue[results$issue=="taxes"] =  b
results$issue[results$issue=="militaryHC"] = a
results$issue[results$issue=="warming"] =e
results$issue[results$issue=="NEP"] = f
results$issue[results$issue=="GMO"] = c
results$issue[results$issue=="foreign_US"] = g
results$issue[results$issue=="unemp_US"] = h

issues = c(h,g,f,e,d,c,b,a)
results$issue = factor(results$issue,levels = issues)
#16 is solid sphere
#15 is solid square
#19 is solid square
#22 is open square
#2 is open triangle
#6 is open diamond
#10 is open sphere with lines
#12 is open square with lines

### Figure 1 ####
results$size = ifelse(results$type=="Government officials",2.4,2.6)
ggplot(data=results, aes(y=as.numeric(mean),x = issue,shape = type,color=party))+ 
  geom_point(position = position_dodge(width = 0, preserve = c("total")), stat='identity',size=results$size)+
  # geom_errorbar(aes(ymin=mean - ci,ymax=mean + ci),position = position_dodge(width = 0, preserve = c("total")), width=0.1,alpha=1)+
  geom_hline(yintercept=0,lwd=0.2,lty=2)+
  scale_color_manual(name = "", values = c("Republican" = ("firebrick"), "Democrat"  = "blue"),
                     guide=guide_legend(nrow=1))+
  scale_shape_manual(name = "", values = c("Public"  = 2,"Government officials" = 15))+
  theme_minimal()+#theme(axis.text.y=element_blank(), axis.ticks.y=element_blank())+
  theme(legend.position="bottom",legend.box = "vertical",
        legend.justification =  0.5, legend.spacing.x = unit(0.1,"cm"),
        legend.spacing.y = unit(-0.5,"cm"))+xlab('')+ 
  ylab('\nAccuracy of government officials and partisans relative to public')+
  coord_flip()+
  ylim(-0.15,0.3) +
  theme(text = element_text(size=11))+
  ggsave("fig_1.pdf",width=7.5, height=6)